<?php

/**
 * 学生管理
 * 
 * @author 陈蕾
 */

class Controller_Admin_Member extends Controller_Admin_Base
{
	// 列表
    public function action_index()
    {
        $count = DB::select(DB::expr("COUNT(*) AS total"))
                ->from('member')
                ->execute()
                ->get('total');
        $pagination = Pagination::factory(array(
            'total_items' => $count
        ));

        $records = DB::select()->from('member')
                ->order_by('id', 'DESC')
                ->limit($pagination->items_per_page)
                ->offset($pagination->offset)
                ->execute()->as_array();

        $this->prepare('学生列表');
        $this->content->records = $records;
        $this->content->pagination = $pagination->render();
        $this->render();
    }
    
	// 添加
    public function action_add()
    {
        if ($_POST) {
            $member = ORM::factory('member');
            $member->need_check_password();
            $member->values($_POST);
            
            /* @var $validate Validate */
            $validate = $member->validate();
            $validate->rule('password_confirm', 'matches', array('password'))
                ->callback('name', array('Model_Member', 'username_available'));

            if ( ! $member->check()) {
                $this->message($validate->errors('member'));
                return;
            }

            if ( ! $member->save()) {
                $this->message('操作失败');
                return;
            }
            $this->message('添加成功', 'member');
        } else {
            $this->prepare('添加学生');
            $this->render();
        }
    }
    
    // 编辑
    public function action_edit()
    {
        if ($_POST) {
            $id = Arr::get($_POST, 'id');

            $member = ORM::factory('member');
            $member->find($id);
            if ( ! $member->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }
            
            unset($_POST['id']);
            
            // 检查格式
            $validate = $member->validate();
            if ($_POST['password']) {
				$member->need_check_password();
				$validate->rule('password_confirm', 'matches', array('password'));
            } else {
            	unset($_POST['password'], $_POST['password_confirm']);
            }
            
            $member->values($_POST);
            
            if ( ! $member->check()) {
                $this->message($validate->errors('member'));
                return;
            }

            // 保存
            if ($member->save()) {
                $this->message('修改成功！', 'member');
            } else {
                $this->message('操作失败！');
            }
        } else {
            $id = (int)$this->request->param('id');

            if (empty($id)) {
                $this->message('参数错误！');
                return;
            }

            $member = ORM::factory('member')->find($id);

            if ( ! $member->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }

            $this->prepare('修改学生');
            $this->content->record = $member->as_array();
            $this->render();
        }
    }

    // 轨迹
    public function action_track()
    {
    	$id = (int)$this->request->param('id');
    	
    	$count = DB::select(DB::expr("COUNT(*) AS total"))
                ->from('lesson_log')
                ->where('member_id', '=', $id)
                ->execute()
                ->get('total');
        $pagination = Pagination::factory(array(
            'total_items' => $count
        ));

        $records = DB::select('ll.*', array('c.name', 'chapter_name'), array('l.name', 'lesson_name'))
        		->from(array('lesson_log', 'll'))
        		->join(array('chapter', 'c'), 'LEFT')
        		->on('ll.chapter_id', '=', 'c.id')
        		->join(array('lesson', 'l'), 'LEFT')
        		->on('ll.lesson_id', '=', 'l.id')
                ->where('member_id', '=', $id)
                ->order_by('id', 'DESC')
                ->limit($pagination->items_per_page)
                ->offset($pagination->offset)
                ->execute()->as_array();

        $this->prepare('学习轨迹');
        $this->content->records = $records;
        $this->content->pagination = $pagination->render();
        $this->render();
    }
}